package com.genersoft.iot.vmp.baidu;

import com.baidu.aip.bodyanalysis.AipBodyAnalysis;
import com.genersoft.iot.vmp.utils.DateUtil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class BaiduTrackApi {

    private static final Logger logger = LoggerFactory.getLogger(BaiduTrackApi.class);
    //设置APPID/AK/SK
    public static final String APP_ID = "45209690";
    public static final String API_KEY = "kQIq0FkMPz9x9W8GYN1xMDLw";
    public static final String SECRET_KEY = "ctkujp3lMLSiU5YMYmyOfZoMEQlcsN2o";



    /**
     * 人体分析-动态
     */
    public static void bodyTracking(String image,Integer count) {
        AipBodyAnalysis client =  new AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("case_id", "1234");
        options.put("case_init", "true");
        options.put("show", "true");
        options.put("area", "1,1,600,1,600,800,1500,800,1500,1,1919,0,1919,1079,1,1079");
        String dynamic = "true";
        JSONObject res = client.bodyTracking(image, dynamic, options);
        try{
            if (!"".equals(res.optString("image"))){
                String s = res.getString("image");
                Base64Util.baseToFile(s,"C:\\Users\\shaom\\Desktop\\video\\image\\snap\\image"+count+".jpg");

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("结束==========="+count+"============"+ DateUtil.getNow());
    }


    /**
     * 人体分析-动态
     */
    public static void bodyTracking(byte[] image,Integer frameCount) {

        System.out.println("开始==========="+frameCount+"============"+ DateUtil.getNow());

        AipBodyAnalysis client =  new AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY);
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("case_id", "1234");
        options.put("case_init", "false");
        options.put("show", "true");
        options.put("area", "1,1,600,1,600,800,1500,800,1500,1,1919,0,1919,1079,1,1079");
        String dynamic = "true";
        JSONObject res = client.bodyTracking(image, dynamic, options);
        try{
            if (!"".equals(res.optString("image"))){
                Base64Util.baseToFile(res.getString("image"),"C:\\Users\\shaom\\Desktop\\video\\image\\snap2\\image"+frameCount+".jpg");
                System.out.println(res.optString("person_count"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("结束==========="+frameCount+"============"+ DateUtil.getNow());
    }


    public static void main(String[] args) {
        try{
            Java2DFrameConverter converter = new Java2DFrameConverter();
            // 创建编码器
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("rtsp://wogou.maihefang.com.cn:554/rtp/34020000001320000001/34020000001320000001");
            grabber.start();
            // 设置帧率
            int frameRate = (int)grabber.getFrameRate(); // 获取原始视频帧率
            int targetFrameRate = 2; // 目标帧率
            int ratio = frameRate / targetFrameRate;
            // 抽帧并保存为图片
            Frame frame;
            int frameCount  = 1;
            AtomicInteger count = new AtomicInteger(1);
            while ((frame = grabber.grabImage()) != null) {
//                // 每隔 ratio 帧抽取一帧
//                if (++frameCount % frameRate == 0) {
//                    // 处理当前帧
//                    BufferedImage image = converter.convert(frame);
//                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
//                    ImageIO.write(image, "jpg", baos);
//                    bodyTracking(baos.toByteArray(), count.getAndIncrement());
//                }
            }
            grabber.stop();
            grabber.release();
        }catch (Exception e){

        }
    }
}
